<?php

namespace app\manage\controller;

use app\common\controller\Manage;
use app\common\model\Liquor as liquorModel;

class Liquor extends Manage
{
    
    public function index()
    {
        return $this->fetch();
    }
    
    public function api()
    {
        $liquor_model = new liquorModel();
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 15);
        $condition = [];
        if($this->request->get('mobile')) {
            $condition[] = ['mobile', '=', $this->request->get('mobile')];
        }
        if($this->request->get('liquor_bn')) {
            $condition[] = ['liquor_bn', '=', $this->request->get('liquor_bn')];
        }
        if($this->request->get('status')) {
            $condition[] = ['status', '=', $this->request->get('status')];
        }
        $liquor_list = $liquor_model::with(['user'])->where($condition)->limit(($page-1)*$limit, $limit)->order('id', 'desc')->select();
        $liquor_count = $liquor_model->where($condition)->count();
        return [
            'code' => 0,
            'count' => $liquor_count,
            'data' => $liquor_list
        ];
    }
    public function add()
    {
        $this->view->engine->layout(false);
        $goods_model = new \app\common\model\Goods();
        $goods_list = $goods_model->where('goods_cat_id', 'in', [41, 42, 43])->field('id, name')->select();
        $this->assign('goods_list', $goods_list);
        return $this->fetch();
    }
    public function save()
    {
        $data = $this->request->post();
        $user_info = model('user')->where('mobile', 'eq', $data['mobile'])->find();
        if(!$user_info) {
            return [
                'status' => false,
                'msg'    => '查无此用户'
            ];
        }
        list($goods_id, $goods_name) = explode('|', $data['goods']);
        $liquor_model = new liquorModel();
        $liquor_model->liquor_bn = un_bn();
        $liquor_model->goods_id = $goods_id;
        $liquor_model->goods_name = $goods_name;
        $liquor_model->user_id = $user_info->id;
        $liquor_model->mobile = $user_info->mobile;
        $liquor_model->number = $data['number'];
        $liquor_model->memo = $data['memo'];
        $liquor_model->save();
        return [
            'status' => true,
            'msg'   => '存入成功'
        ];
    }
    public function edit()
    {
        $this->view->engine->layout(false);
        $goods_model = new \app\common\model\Goods();
        $goods_list = $goods_model->where('goods_cat_id', 'in', [41, 42, 43])->field('id, name')->select();
        $this->assign('goods_list', $goods_list);
        $liquor_model = new liquorModel();
        $liquor_info = $liquor_model->get($this->request->get('id'));
        $this->assign('info', $liquor_info);
        return $this->fetch();
    }
    
    public function update()
    {
        $data = $this->request->post();
        list($data['goods_id'], $data['goods_name']) = explode('|', $data['goods']);
        $liquor_model = new liquorModel();
        $liquor_model->isUpdate()->save($data);
        return [
            'status' => true,
            'msg'   => '修改成功'
        ];
    }
    
    public function cancel()
    {
        $liquor_model = new liquorModel();
        $data = [
            'id'        => $this->request->post('id'),
            'status'    => 'cancel' 
        ];
        $liquor_model->isUpdate()->save($data);
        return [
            'status' => true,
            'msg'   => '修改成功'
        ];
    }
}
